package com.example.hotel_lxy.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.example.hotel_lxy.R;
import com.example.hotel_lxy.adapter.CourseMenuAdapter;
import com.example.hotel_lxy.application.ARouterPath;
import com.example.hotel_lxy.bean.BeanBean;
import com.example.hotel_lxy.bean.HelpCenterBean;
import com.example.hotel_lxy.net.Api;
import com.example.hotel_lxy.utils.SPUtils;
import com.ysxsoft.common_base.base.BaseActivity;
import com.ysxsoft.common_base.base.RBaseAdapter;
import com.ysxsoft.common_base.base.RViewHolder;
import com.ysxsoft.common_base.utils.JsonUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;

/**
 * 帮助中心
 */
@Route(path = "/main/HelpCenterActivity")
public class HelpCenterActivity extends BaseActivity implements View.OnClickListener {
    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.rv_help)
    ExpandableListView rvHelp;
    @BindView(R.id.screen)
    EditText screen;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;


    public static void start() {
        ARouter.getInstance().build(ARouterPath.getHelpCenterActivity())
                .navigation();
    }

    @Override
    public void doWork() {
        super.doWork();
        back.setOnClickListener(this);
        showData();
        screen.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (!s.toString().equals("")) {
                    recyclerview.setVisibility(View.VISIBLE);
                    rvHelp.setVisibility(View.GONE);
                    showDataInfo(s.toString().trim());
                }else{
                    recyclerview.setVisibility(View.GONE);
                    rvHelp.setVisibility(View.VISIBLE);
                }
            }
        });
    }

    private void showDataInfo(String trim) {
        String token = SPUtils.get(HelpCenterActivity.this, "token", "").toString();
        OkHttpUtils.post()
                .url(Api.POST_VIEWSET)
                .addHeader("flag", token)
                .addParams("search", "" + trim)
                .tag(this)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Log.e("TAG", "帮助中心Exception~~~~~~~~    " + e.getMessage());
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Log.e("TAG", "帮助中心Exception~~~~~~~~    " + response);
                        BeanBean beanBean = JsonUtils.parseByGson(response, BeanBean.class);
                        if (beanBean.getCode().equals("4")) {
                            SPUtils.put(HelpCenterActivity.this, "token", "");
                            Intent intent = new Intent(HelpCenterActivity.this, LoginActivity.class).
                                    setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                            startActivity(intent);
                            return;
                        }
                        if (beanBean.getCode().equals("0")) {
                            HelpCenterBean helpCenterBean = JsonUtils.parseByGson(response, HelpCenterBean.class);
                            recyclerview.setLayoutManager(new LinearLayoutManager(HelpCenterActivity.this));
                            RBaseAdapter<HelpCenterBean.DataBean.ListBean> rBaseAdapter=new RBaseAdapter<HelpCenterBean.DataBean.ListBean>(HelpCenterActivity.this,
                                    R.layout.item_help_activity,helpCenterBean.getData().getList()) {
                                @Override
                                protected void fillItem(RViewHolder holder, HelpCenterBean.DataBean.ListBean item, int position) {
                                    TextView text1=holder.getView(R.id.text1);
                                    text1.setText(item.getTitle());
                                    holder.itemView.setOnClickListener(new View.OnClickListener() {
                                        @Override
                                        public void onClick(View v) {
                                            WebHelpActivity.start(item.getId()+"", item.getTitle());
                                        }
                                    });
                                }

                                @Override
                                protected int getViewType(HelpCenterBean.DataBean.ListBean item, int position) {
                                    return 0;
                                }
                            };
                            recyclerview.setAdapter(rBaseAdapter);
                        }
                    }
                });
    }

    private void showData() {
        String token = SPUtils.get(HelpCenterActivity.this, "token", "").toString();
        OkHttpUtils.post()
                .url(Api.POST_VIEWSET)
                .addHeader("flag", token)
                .tag(this)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Log.e("TAG", "帮助中心Exception~~~~~~~~    " + e.getMessage());
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Log.e("TAG", "帮助中心Exception~~~~~~~~    " + response);
                        BeanBean beanBean = JsonUtils.parseByGson(response, BeanBean.class);
                        if (beanBean.getCode().equals("4")) {
                            SPUtils.put(HelpCenterActivity.this, "token", "");
                            Intent intent = new Intent(HelpCenterActivity.this, LoginActivity.class).
                                    setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                            startActivity(intent);
                            return;
                        }
                        if (beanBean.getCode().equals("0")) {
                            HelpCenterBean helpCenterBean = JsonUtils.parseByGson(response, HelpCenterBean.class);
                            List<HelpCenterBean.DataBean.ListBean> list = helpCenterBean.getData().getList();
                            CourseMenuAdapter courseMenuAdapter = new CourseMenuAdapter(HelpCenterActivity.this, list);
                            rvHelp.setAdapter(courseMenuAdapter);
                            rvHelp.setGroupIndicator(null);
                            //设置子项布局监听
                            rvHelp.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
                                @Override
                                public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                                    WebHelpActivity.start(list.get(groupPosition).getLists().get(childPosition).getId() + "", list.get(groupPosition).getLists().get(childPosition).getTitle());
                                    return true;

                                }
                            });
                            rvHelp.expandGroup(0);
                            //控制他只能打开一个组
                            rvHelp.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
                                @Override
                                public void onGroupExpand(int groupPosition) {
                                    int count = new CourseMenuAdapter(list).getGroupCount();
                                    for (int i = 0; i < count; i++) {
                                        if (i != groupPosition) {
                                            rvHelp.collapseGroup(i);
                                        }
                                    }
                                }
                            });
                        }

                    }
                });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_help_center;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back:
                finish();
                break;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}